﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using Model.Sys;
using Model.Sys.Config;
using Model.Sys.Constant;
using IBll;

namespace Controllers.Extend
{
    /// <summary>
    /// 基础控制器类（“站点信息”的读取）
    /// </summary>
    public class BaseController : Controller
    {
        public IBllSession CurrentBllSession { get; set; }

        /// <summary>
        /// 站点配置信息
        /// </summary>
        protected SiteConfig config;

        /// <summary>
        /// 执行前读取站点配置信息
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Cache cache = CurrentBllSession.CacheBll.GetCache(Constant.CacheKey.SiteConfigCacheKey);//从缓存中获取站点信息,若缓存中没有，则从缓存文件中读取

            if (cache != null)
            {
                this.config = (SiteConfig)cache.Value;
            }
            else
            {
                this.config = null;
            }
            if (this.config == null)//若缓存和缓存文件中无站点信息，则通过读取“站点信息”配置文件信息到SiteConfig对象中，保存对象到缓存并写入缓存文件
            {
                this.config = CurrentBllSession.SiteConfigBll.LoadConfig(Constant.PhysicalPath.SiteConfigPhysicalPath);
                CurrentBllSession.CacheBll.Add(Constant.CacheKey.SiteConfigCacheKey, config);
            }
            if (this.config.SiteStatus == false) //关闭站点
            {
                filterContext.Result = new RedirectResult("/Error/SiteClose");
            }
        }

        /// <summary>
        /// 返回结果前附加数据
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (this.config != null)//给视图设置站点信息
            {
                ViewData["SiteConfig"] = this.config;
            }
        }

    }
}
